package org.example.dao;

import org.example.exception.AppException;
import org.example.model.User;
import org.example.util.Util;

import java.sql.*;
import java.util.Date;

public class UserDao {

    //jdbc操作

    public static User queryByName(String name) {
        Connection c = null;
        PreparedStatement p = null;
        ResultSet r = null;
        User user = null;

        try{
            //1.获取数据库连接Connection
            c = Util.getConnection();
            //2.通过Connection+sql创建操作命令对象Statement
            String sql = "select * from user where name = ?";
            //3.执行sql; 执行前替换占位符
            p = c.prepareStatement(sql);
            //4.如果是查询操作，则需要处理结果集
            p.setString(1,name); //这里根据用户name进行查询
            //System.out.println(p);
            r = p.executeQuery();
            while(r.next()){
                user = new User();
                //设置结果集字段到用户对象的属性中
                user.setUserId(r.getInt("userId"));
                user.setName(name);
                user.setPassword(r.getString("password"));
                user.setNickName(r.getString("nickName"));
                user.setIconPath(r.getString("iconPath"));
                user.setSignature(r.getString("signature"));

                Timestamp lastLogout = r.getTimestamp("lastLogout");
                user.setLastLogout(new Date(lastLogout.getTime()));
            }
            return user;  //如果发生异常，user为初始值null
        }catch(Exception e){
            throw new AppException("查询账号出错",e);
        }finally{
            //5.释放资源
            Util.close(c,p,r);
        }

    }

    public static int updateLastLogout(Integer userId) {
        Connection c = null;
        PreparedStatement p = null;
        try{
            c = Util.getConnection(); //获取数据库连接对象
            String sql = "update user set lastLogout = ? where userId = ?";
            p = c.prepareStatement(sql);  //创建预编译的操作命令对象
            p.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
            p.setInt(2,userId);
            return p.executeUpdate();
        }catch(Exception e){
            throw new AppException("修改用户最近上次的登录时间出错！");
        }finally{
            Util.close(c,p);
        }
    }

    public static int insertUser(User user){
        Connection c = null;
        PreparedStatement p = null;

        try {
            c = Util.getConnection(); //获得数据库连接对象
            String sql = "insert into user(name,password,nickName,iconPath,signature,lastLogout) value(?,?,?,?,?,?)";
            p = c.prepareStatement(sql);
            p.setString(1,user.getName());
            p.setString(2,user.getPassword());
            p.setString(3,user.getNickName());
            p.setString(4,"");
            p.setString(5,user.getSignature());
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            p.setTimestamp(6,timestamp);

//            user.setIconPath("");
//            user.setLastLogout(timestamp);
            return p.executeUpdate();
        } catch (Exception e) {
            throw new AppException("插入用户失败",e);
        }finally{
            Util.close(c,p);
        }
    }
}
